import 'package:pcms/models/air_conditioner_status.dart';
import 'package:pcms/services/api_service.dart';

/// 获取XF-01空调机组信息
/// 从API获取实时数据，失败时返回默认数据
Future<List<AirConditionerStatus>> getXf01Data() async {
  try {
    final data = await ApiService.getPointDataList(
      moduleName: '运行状态',
      category: 'XF-01空调机组信息表',
    );
    
    // 将API数据转换为AirConditionerStatus对象
    return data.map<AirConditionerStatus>((item) {
      return AirConditionerStatus(
        label: item['name'] ?? item['label'] ?? '未知设备',
        address: item['address'] ?? item['tag'] ?? 'Unknown',
        status: item['status'] ?? item['value'] ?? false,
      );
    }).toList();
  } catch (e) {
    print('获取XF-01空调机组信息失败: $e');
    // 返回默认的mock数据作为fallback
    // return _getDefaultXf01Data();
    return [];
  }
}

/// 获取XF-02空调机组信息
/// 从API获取实时数据，失败时返回默认数据
Future<List<AirConditionerStatus>> getXf02Data() async {
  try {
    final data = await ApiService.getPointDataList(
      moduleName: '运行状态',
      category: 'XF-02空调机组信息表',
    );
    print('XF-02空调机组API数据: $data');
    
    // 将API数据转换为AirConditionerStatus对象
    return data.map<AirConditionerStatus>((item) {
      return AirConditionerStatus(
        label: item['name'] ?? item['label'] ?? '未知设备',
        address: item['address'] ?? item['tag'] ?? 'Unknown',
        status: item['status'] ?? item['value'] ?? false,
      );
    }).toList();
  } catch (e) {
    print('获取XF-02空调机组信息失败: $e');
    // 返回默认的mock数据作为fallback
    //return _getDefaultXf02Data();
    return [];
  }
}

/// 默认XF-01空调机组数据
List<AirConditionerStatus> _getDefaultXf01Data() {
  return [
    const AirConditionerStatus(label: '排风机', address: 'DB3.DBX42.0', status: false),
    const AirConditionerStatus(label: '送风机', address: 'DB3.DBX42.1', status: false),
    const AirConditionerStatus(label: '加湿器', address: 'DB3.DBX42.2', status: false),
    const AirConditionerStatus(label: '消毒机', address: 'DB3.DBX42.3', status: false),
    const AirConditionerStatus(label: '预加热', address: 'DB3.DBX54.1', status: false),
    const AirConditionerStatus(label: '预加热', address: 'DB3.DBX54.2', status: false),
    const AirConditionerStatus(label: '预加热', address: 'DB3.DBX54.3', status: false),
    const AirConditionerStatus(label: '电加热', address: 'DB3.DBX54.4', status: false),
    const AirConditionerStatus(label: '电加热', address: 'DB3.DBX54.5', status: false),
    const AirConditionerStatus(label: '电加热', address: 'DB3.DBX54.6', status: false),
  ];
}

/// 默认XF-02空调机组数据
List<AirConditionerStatus> _getDefaultXf02Data() {
  return [
    const AirConditionerStatus(label: '排风机', address: 'DB4.DBX42.0', status: false),
    const AirConditionerStatus(label: '送风机', address: 'DB4.DBX42.1', status: false),
    const AirConditionerStatus(label: '加湿器', address: 'DB4.DBX42.2', status: false),
    const AirConditionerStatus(label: '消毒机', address: 'DB4.DBX42.3', status: false),
    const AirConditionerStatus(label: '预加热', address: 'DB4.DBX54.1', status: false),
    const AirConditionerStatus(label: '预加热', address: 'DB4.DBX54.2', status: false),
    const AirConditionerStatus(label: '预加热', address: 'DB4.DBX54.3', status: false),
    const AirConditionerStatus(label: '电加热', address: 'DB4.DBX54.4', status: false),
    const AirConditionerStatus(label: '电加热', address: 'DB4.DBX54.5', status: false),
    const AirConditionerStatus(label: '电加热', address: 'DB4.DBX54.6', status: false),
  ];
}